<?php
header('Content-Type: text/html;charset=utf-8');
header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型
header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin'); // 设置允许自定义请求头的字段

// 假定数据库用户名：root，密码：root，数据库：khy 
$con=mysqli_connect("localhost","root","root","khy"); 
if (mysqli_connect_errno($con)) 
{ 
    echo "连接 MySQL 失败: " . mysqli_connect_error(); 
} 

// 修改数据库连接字符集为 utf8
mysqli_set_charset($con,"utf8mb4");

//获取更新的数据的id
$id = $_GET['id'];

// 接受表单提交过来的数据
$post = $_POST;

// var_dump($post);exit;

$name = $post['name'];
$sex = $post['sex'];
$age = $post['age'];


$sql ="UPDATE `user` SET `name`='$name',`sex`='$sex',`age`=$age WHERE id = $id"; //SQL语句
// var_dump($sql); exit;

$result = mysqli_query($con,$sql); //执行查询结果

$sql1 ="select * from `user` where `id` = $id"; //查询对应的id的数据

$result1 = mysqli_query($con,$sql1); //执行查询结果

$row1 = mysqli_fetch_assoc($result1);//把查询结果转化为数组

$return = [
    'code' => 1,
    'msg' => '更新数据成功！',
    'data' => $row1
];

$data = json_encode($return);//把数据转为json格式
// var_dump($data);
echo $data;